package com.scrcu.core.batch.quartz;

import com.scrcu.core.batch.BatchContext;
import com.scrcu.core.batch.service.BatchDispatcher;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用于定时调起流程的Quartz Job子类<br/>
 * 
 */
public class SimpleBatchQuartzJob implements Job {
	private static final Logger log = LoggerFactory.getLogger(SimpleBatchQuartzJob.class);

	/**
	 * 调起指定的流程
	 */
	@Override
	public final void execute(JobExecutionContext jec) throws JobExecutionException {
		JobKey jk = jec.getJobDetail().getKey();
		String impl = jk.getGroup(); // BatchInfo.impl / dispatcher
		String id = jk.getName(); // BatchInfo.id
		try {
			BatchDispatcher bd = BatchContext.getDispatcher(impl);
			if (bd == null) {
				log.warn("Can not find batch dispatcher {}", impl);
				return;
			}
			log.info("Trigger batch {} with dispatcher {}", id, impl);
			bd.start(id);
		} catch (Throwable e) {
			log.error("Error on invoke batch {}", id, e);
		}
	}
}
